package com.ticket.system.service.impl;

import java.util.List;

import com.ticket.system.domain.SysNoticeUser;
import com.ticket.system.mapper.SysNoticeUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ticket.system.domain.SysNotice;
import com.ticket.system.mapper.SysNoticeMapper;
import com.ticket.system.service.ISysNoticeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公告 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;
    @Autowired
    private SysNoticeUserMapper sysNoticeUserMapper;


    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    @Override
    public List<SysNotice> selectNoticeUserList(SysNotice notice)
    {
        return noticeMapper.selectNoticeUserList(notice);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNotice(SysNotice notice,Long userId)
    {
        int count = noticeMapper.insertNotice(notice);
        if(count>0){
            sysNoticeUserMapper.insertBatch(notice.getNoticeId(),notice.getForceRemind(),notice.getPushChannel());
        }
        return count;
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        int num = noticeMapper.updateNotice(notice);
        if(num > 0 && notice.getForceRemind().equals(1)){
            SysNoticeUser user = new SysNoticeUser();
            user.setNoticeId(notice.getNoticeId());
            user.setRemind(1);
            sysNoticeUserMapper.updateSysNoticeUser(user);
        }
        return num;
    }

    /**
     * 删除公告对象
     * 
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId)
    {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     * 
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        int count = noticeMapper.deleteNoticeByIds(noticeIds);
        if(count>0){
            sysNoticeUserMapper.deleteByNoticeId(noticeIds);
        }
        return count;
    }
}
